package com.xxh.food.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxh.food.common.PagePARAM;
import com.xxh.food.common.Result;
import com.xxh.food.common.entity.Online;
import com.xxh.food.common.entity.User;
import com.xxh.food.common.param.OnlinePARAM;
import com.xxh.food.common.param.UserPARAM;
import com.xxh.food.service.OnlineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.LocalTime;

@RestController
@RequestMapping(value = "/online")
@Api(tags = "留言模块接口")
public class OnlineController {
    @Resource
    private OnlineService onlineService;

    @ApiOperation(value = "查询留言总数")
    @GetMapping(value = "/num")
    public Result<?> getOnlineNum(){
        int num = onlineService.onlineNum();
        if (num > 0) {
            return Result.success(num);
        } else {
            return Result.error("-1", "查询失败");
        }
    }


    @ApiOperation(value = "添加留言")
    @PostMapping(value = "/add")
    public Result<?> addOnline(@RequestBody OnlinePARAM onlinePARAM){
        Online online = new Online();
        BeanUtils.copyProperties(onlinePARAM,online);
        online.setCreateTime(LocalDateTime.now().toString());
        onlineService.save(online);
        return Result.success(online);
    }

    @ApiOperation(value = "无筛选分页查询")
    @GetMapping("/{page}/{size}")
    public Result<?> findUserByPage(@PathVariable Integer page,@PathVariable Integer size){

        Page<Online> onlinePage = onlineService.page(new Page<Online>(page,size));
        return Result.success(onlinePage);
    }

}
